Files are saved when changing views

James Peret 8 years ago
parent
commit
9299ed3225

+ 2 - 0
app/scripts/controllers/header-ctrl.js

@@ -17,6 +17,7 @@ angular.module('codexApp.header', [])
17 17
 
18 18
     $scope.activateNoteView = function() {
19 19
       $rootScope.$broadcast('activate-note-view');
20
+      $rootScope.$broadcast('window-view:change');
20 21
       $state.go("note-view");
21 22
       $scope.noteViewBtnClass = "active";
22 23
       $scope.noteEditBtnClass = "";
@@ -24,6 +25,7 @@ angular.module('codexApp.header', [])
24 25
 
25 26
     $scope.activateNoteEdit = function() {
26 27
       $rootScope.$broadcast('activate-note-edit');
28
+      $rootScope.$broadcast('window-view:change');
27 29
       $state.go("note-edit");
28 30
       $scope.noteViewBtnClass = "";
29 31
       $scope.noteEditBtnClass = "active";

+ 6 - 21
app/scripts/controllers/note-edit-ctrl.js

@@ -20,6 +20,7 @@ angular.module('codexApp.noteEdit', [])
20 20
     $scope.raw_data = "";
21 21
     $scope.showNoteView = true;
22 22
     $scope.showNoteEdit = false;
23
+    $scope.savedBeforeQuit = false;
23 24
 
24 25
 
25 26
     filesystem.readFile($scope.note.path, function(err, data) {
@@ -56,31 +57,15 @@ angular.module('codexApp.noteEdit', [])
56 57
       return marked(str);
57 58
     }
58 59
 
59
-    $rootScope.$on('activate-note-view', function() {
60
-      if(!$scope.$$phase) {
61
-        $scope.$apply(function(){
62
-          $scope.showNoteView = true;
63
-          $scope.showNoteEdit = false;
64
-        });
65
-      } else {
66
-        $scope.showNoteView = true;
67
-        $scope.showNoteEdit = false;
60
+    $rootScope.$on('window-view:change', function() {
61
+      if($scope.savedBeforeQuit == false) {
62
+        $scope.savedBeforeQuit = true;
63
+        FileService.saveFile($scope.note.path, $scope.raw_data)
68 64
       }
69
-    });
70 65
 
71
-    $rootScope.$on('activate-note-edit', function() {
72
-      if(!$scope.$$phase) {
73
-        $scope.$apply(function(){
74
-          $scope.showNoteView = false;
75
-          $scope.showNoteEdit = true;
76
-        });
77
-      } else {
78
-        $scope.showNoteView = false;
79
-        $scope.showNoteEdit = true;
80
-      }
81
-      //console.log($scope.raw_data);
82 66
     });
83 67
 
68
+
84 69
     $scope.aceLoaded = function(_editor) {
85 70
        _editor.setReadOnly(false);
86 71
       console.log($scope.raw_data);

+ 0 - 9
app/scripts/controllers/note-view-ctrl.js

@@ -79,13 +79,4 @@ angular.module('codexApp.noteView', [])
79 79
       //console.log($scope.raw_data);
80 80
     });
81 81
 
82
-    $scope.aceLoaded = function(_editor) {
83
-       _editor.setReadOnly(false);
84
-      console.log($scope.raw_data);
85
-    };
86
-
87
-    $scope.aceChanged = function(e) {
88
-      console.log("-> Note data changed.");
89
-    };
90
-
91 82
   }]);

+ 1 - 0
app/scripts/controllers/sidebar-ctrl.js

@@ -14,6 +14,7 @@ angular.module('codexApp.sidebar', [])
14 14
 
15 15
     $scope.goToAllNotes = function() {
16 16
       $rootScope.$broadcast('main-window:note-list');
17
+      $rootScope.$broadcast('window-view:change');
17 18
       $state.go("index");
18 19
     }
19 20
 

+ 11 - 0
app/scripts/services/file-service.js

@@ -159,6 +159,17 @@ angular.module('codexApp')
159 159
     return results[0];
160 160
   };
161 161
 
162
+  this.saveFile = function(file_path, content){
163
+    var fs = require('fs');
164
+    fs.writeFile(file_path, content, function(err) {
165
+        if(err) {
166
+            return console.log(err);
167
+        }
168
+
169
+        console.log("-> FILE SAVED: " + file_path);
170
+    });
171
+  }
172
+
162 173
 
163 174
   // RESPONSE
164 175
   this.getNotes = function() {

+ 6 - 0
codex/Test-Stack/Notebook 2/Test-0002/index.md

@@ -1,3 +1,9 @@
1 1
 # Test #0002
2 2
 
3 3
 Testing the codex app note structure. This is an example note.
4
+
5
+Will it Save?
6
+It Saves! Its so awesome!
7
+
8
+does it still save?
9
+even if i change notes thru the sidebar?